package com.alien.okcoin.servieImpl;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alien.okcoin.WebSocketBase;
import com.alien.okcoin.WebSocketService;
import com.alien.okcoin.bean.OkSubSpotBtcDepth;
import com.alien.okcoin.utils.Utils;
/**
 * 订阅信息处理类需要实现WebSocketService接口
 * @author okcoin
 *
 */
public class BuissnesWebSocketServiceImpl implements WebSocketService{
	private Logger log = Logger.getLogger(WebSocketBase.class);
	@Override
	public void onReceive(String msg){
		log.debug("onReceive:"+msg);
		if (msg.startsWith(Utils.getChannelJsonHeader("ok_sub_spotusd_btc_depth"))
				& msg.contains("data")) {
			OkSubSpotBtcDepth okSubSpotBtcDepth = JSON.parseObject(Utils.JsonRevise(msg), OkSubSpotBtcDepth.class);
			
			List<List<Double>> bids = okSubSpotBtcDepth.getData().getBids();
			double bidsPrice=0.0;
			double bidsAmount=0;
			for (int i = 0; i < bids.size(); i++) {
				bidsPrice =bidsPrice + bids.get(i).get(0);
				bidsAmount = bidsAmount + bids.get(i).get(1);
			}
			log.debug("bidsPrice sum:"+bidsPrice + "  bidsAmount sum:"+bidsAmount);
			
			List<List<Double>> asks = okSubSpotBtcDepth.getData().getAsks();
			double asksPrice = 0.0;
			double asksAmount= 0;
			for (int i = 0; i < asks.size(); i++) {
				asksPrice = asksPrice + asks.get(i).get(0);
				asksAmount = asksAmount + asks.get(i).get(1);
			}
			log.debug("asksPrice sum:"+asksPrice + "  asksAmount sum:"+asksAmount);
			
			log.info("卖方挂单价格总和 - 买方挂单价格总和："+(asksPrice-bidsPrice));
			log.info("卖方挂单总量 - 买方挂单总量"+(asksAmount-bidsAmount));
		}
	}
}
